#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
cron: 30 8 * * *
new Env('每日任务汇总推送')
功能：收集所有脚本生成的dailylog文件，合并内容并发送汇总消息
"""

import os
import glob
from notify import send
import datetime

def main():
    # 获取当前脚本所在目录
    current_dir = os.path.dirname(os.path.abspath(__file__))
    
    # 收集所有脚本同级目录的dailylog文件
    log_pattern = os.path.join(current_dir, 'dailylog')
    log_files = glob.glob(log_pattern)
    
    content = []
    
    # 读取所有日志文件内容
    for f in log_files:
        try:
            with open(f, encoding="utf-8") as fd:
                file_content = fd.read()
                if file_content.strip():
                    content.append(file_content)
            # 日清：删除已读取的日志文件
            os.remove(f)
        except Exception as e:
            print(f"处理日志文件 {f} 时出错: {e}")

    # 发送汇总消息
    if content:
        # 合并所有内容
        final_content = "\n".join(content)
        today = datetime.date.today().strftime("%m-%d")
        if content:
            send(f"📋 {today}任务汇总 ", final_content)
        else:
            send(f"📋 {today}任务汇总 ", "今日无日志")

if __name__ == "__main__":
    main()